package com.wanxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.CustomerModel;
import com.wanxi.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Li
 * @Date 2023/7/8 21:01
 */
@RestController
@RequestMapping("/back/customer")
@Api(tags = "客户信息管理",value = "OPenApi")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @GetMapping("findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询客户信息",response = CustomerModel.class,httpMethod = "GET")
    public ResultDto findAll(CustomerModel customerModel) {
        Page<CustomerModel> page = new Page<>(customerModel.getPage(), customerModel.getLimit());
        QueryWrapper<CustomerModel> queryWrappers = query(customerModel);
        IPage<CustomerModel> iPage = customerService.page(page, queryWrappers);
        return ResultDto.builder()
                .code(0)
                .data(iPage.getRecords())
                .count((int) iPage.getTotal())
                .build();
    }



    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询客户信息",response = CustomerModel.class,httpMethod = "GET")
    public ResultDto findById(CustomerModel customerModel) {
        CustomerModel customer = customerService.getById(customerModel.getId());
        return ResultDto.builder()
                .code(0)
                .data(customer)
                .build();
    }

    @PostMapping("/addCustomer")
    @PreAuthorize("hasAnyAuthority('add')")//权限
    @ApiOperation(value = "新增客户信息",response = CustomerModel.class,httpMethod = "POST")
    public ResultDto addCustomer(CustomerModel customerModel) {
        customerService.save(customerModel);
        return ResultDto.builder()
                .code(0)
                .msg("新增成功！")
                .build();
    }

    @PostMapping("/updateCustomer")
    @PreAuthorize("hasAnyAuthority('update')")//权限
    @ApiOperation(value = "修改客户信息",response = CustomerModel.class,httpMethod = "POST")
    public ResultDto updateCustomer(CustomerModel customerModel) {
        customerService.updateById(customerModel);
        return ResultDto.builder()
                .code(0)
                .msg("修改成功！")
                .build();
    }

    @PostMapping("/deleteCustomer")
    @PreAuthorize("hasAnyAuthority('delete')")//权限
    @ApiOperation(value = "删除客户信息",response = CustomerModel.class,httpMethod = "POST")
    public ResultDto deleteCustomer(CustomerModel customerModel) {
        customerService.removeById(customerModel.getId());
        return ResultDto.builder()
                .code(0)
                .msg("删除成功！")
                .build();
    }

    private QueryWrapper<CustomerModel> query(CustomerModel customerModel) {
        QueryWrapper<CustomerModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(customerModel.getName()==null),"name",customerModel.getName());
        queryWrapper.like(!(customerModel.getSex()==null),"sex",customerModel.getSex());
        if(customerModel.getStartAge()!=null&&customerModel.getEndAge()==null){
            queryWrapper.ge("age",customerModel.getStartAge());
        }else if (customerModel.getEndAge()!=null&&customerModel.getStartAge()==null){
            queryWrapper.le("age",customerModel.getEndAge());
        }else {
            queryWrapper.between(!(customerModel.getStartAge()==null&&customerModel.getEndAge()==null),"age",customerModel.getStartAge(),customerModel.getEndAge());
        }
        queryWrapper.like(!(customerModel.getPhone()==null),"phone",customerModel.getPhone());
        queryWrapper.like(!(customerModel.getAddress()==null),"address",customerModel.getAddress());
        queryWrapper.eq(customerModel.getEnable()!=null&&customerModel.getEnable()!=-1,"enable",customerModel.getEnable());
        queryWrapper.like(!(customerModel.getEmail()==null),"email",customerModel.getEmail());
        return queryWrapper;
    }
}
